package com.bj.dz.controller;

import com.bj.dz.entity.ReleaseTask;
import com.bj.dz.entity.TaskRecord;
import com.bj.dz.service.TaskRecordService;
import com.bj.dz.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
*   用户任务上传记录控制器
*/
@Slf4j
@Api(tags = "【用户任务上传记录】提供服务接口")
@RestController
@RequestMapping("/taskRecord")
public class TaskRecordController {

    @Resource
    private TaskRecordService taskRecordService;

    /**
    * 通过id查询
    * @param
    * @return
    */
    @ApiOperation(value = "查找服务")
    @RequestMapping(value="/findById",method = RequestMethod.POST, produces = "application/json;" +"charset=UTF-8")
    public TaskRecord findTaskRecordById(@ApiParam(name = "id",value = "TaskRecord的id",required = true) @RequestParam(value = "id",required=true)Integer id){
        return taskRecordService.getTaskRecordById(id);
    }

    /**
     * 查询所有发布任务【根据模块(1建筑物2水3汽4交通5电力6通讯)以及状态为1(发布的)-----7(入库状态)】【使用2】
     * 通过模块type和状态state【使用2】
     * 没有传入状态查询所有
     *
     * @param
     * @return
     */
    @ApiOperation(value = "根据id查询任务的上传历史")
    @RequestMapping(value = "/findAllByTaskId", method = RequestMethod.GET, produces = "application/json;" + "charset=UTF-8")
    public Page findAllByType(@ApiParam(name = "taskId", value = "查询的任务id", required = false) @RequestParam(value = "taskId", required = false) Integer taskId,
                              @ApiParam(name = "page", value = "当前页", required = true) @RequestParam(value = "page", required = true) Integer page,
                              @ApiParam(name = "rows", value = "每页显示行数", required = true) @RequestParam(value = "rows", required = true) Integer rows) {
        log.info("查询的任务id是:" + taskId);
        //查询所有发布任务根据模块类型
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("taskId", taskId);//模块类型
        //param.put("State", Constant.RELEASE_TASK_STATE_1);//状态为1
        Page<TaskRecord> pageData = taskRecordService.queryTaskRecordPageByMap(param, page, rows);//分页相关的一切数据
        return pageData;
    }

    /**
    * 通过多个查询
    * @param
    * @return
    */
    @ApiOperation(value = "查找多个服务")
    @RequestMapping(value="/findTaskRecords",method = RequestMethod.POST, produces = "application/json;" +"charset=UTF-8")
    public List<TaskRecord> findTaskRecordListByMap(@ApiParam(name = "TaskRecord",value = "taskRecord",required = true)@RequestBody TaskRecord taskRecord){

        return taskRecordService.getTaskRecordListByMap(null);
    }

    /**
    * 查询总数
    * @param
    * @return
    */
    @ApiOperation(value = "获取总数服务")
    @RequestMapping(value="/count",method = RequestMethod.POST, produces = "application/json;" +"charset=UTF-8")
    public Integer TaskRecordCount(@ApiParam(name = "taskId", value = "任务id", required = true) @RequestParam(value = "taskId", required = true) Integer taskId) {
        log.info("查询上传历史的任务id为:" + taskId);
        //查询所有发布任务根据模块类型
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("taskId", taskId);//模块类型
        return taskRecordService.getTaskRecordCountByMap(param);
    }

    /**
    * 添加保存
    * @param
    * @return
    */
    @ApiOperation(value = "添加服务")
    @RequestMapping(value="/add",method = RequestMethod.POST, produces = "application/json;" +"charset=UTF-8")
    public boolean addTaskRecord(@ApiParam(name = "TaskRecord",value = "taskRecord对象",required = true)@RequestBody TaskRecord taskRecord){
            return taskRecordService.addTaskRecord(taskRecord);
    }

    /**
    * 更新修改
    * @param
    * @return
    */
    @ApiOperation(value = "更新修改服务")
    @RequestMapping(value="/upd",method = RequestMethod.POST, produces = "application/json;" +"charset=UTF-8")
    public boolean modifyTaskRecord(@ApiParam(name = "TaskRecord",value = "taskRecord对象",required = true)@RequestBody TaskRecord taskRecord){
        return taskRecordService.modifyTaskRecord(taskRecord);
    }

    /**
    * 删除
    * @param
    * @return
    */
    @ApiOperation(value = "删除服务")
    @RequestMapping(value="/del",method = RequestMethod.POST, produces = "application/json;" +"charset=UTF-8")
    public boolean deleteTaskRecordById(@ApiParam(name = "TaskRecord",value = "taskRecord的id",required = true) @RequestParam(value = "id",required=true)Integer id){
        return taskRecordService.deleteTaskRecordById(id);
    }
    @ApiOperation(value = "分页查询服务")
    @RequestMapping(value="/queryTaskRecords",method = RequestMethod.POST, produces = "application/json;" +"charset=UTF-8")
    public Page<TaskRecord> queryTaskRecordPageByMap(@RequestParam Map<String, Object> paramMap,
                                                     @RequestParam (name = "currPage", required = true)Integer currPage,
                                                     @RequestParam (name = "type", required = true)Integer type){
        Integer pageSize = 2;
        currPage = currPage != null?currPage : 1;
        paramMap.put("type",type);
        return taskRecordService.queryTaskRecordPageByMap(paramMap,currPage,pageSize);
    }

}
